home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
jed096_1.zip
/
SLANG
/
SRC
/
SLSTD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1994-04-26
|
36KB
|
1,317 lines
/* Basic string functions for S-Lang */
/*
* Copyright (c) 1992, 1994 John E. Davis
* All rights reserved.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL JOHN E. DAVIS BE LIABLE TO ANY PARTY FOR DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
* THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF JOHN E. DAVIS
* HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* JOHN E. DAVIS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
* BASIS, AND JOHN E. DAVIS HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
* SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include <stdio.h>
#ifdef __cplusplus
#include <stdlib.h>
#else
extern int system();
#endif
#include "slang.h"
#include "_slang.h"
#include "slarray.h"
/* Standard intrinsic functions for S-Lang. Included here are string
and array operations */
/* builtin stack manipulation functions */
void SLdo_pop(void)
{
SLang_Object_Type x;
if (SLang_pop(&x)) return;
if (IS_DATA_STRING(x)) FREE(x.v.s_val);
}
int SLdo_dup(void)
{
SLang_Object_Type x;
if (SLang_pop(&x)) return(0);
SLang_push(&x);
if ((x.type >> 8) == STRING_TYPE) SLang_push_string(x.v.s_val);
else SLang_push (&x);
return(1);
}
void SLdo_strcat(void)
{
char *a, *b, *c;
int len, lena;
int adata, bdata;
if (SLang_pop_string(&b, &bdata) || SLang_pop_string(&a, &adata)) return;
lena = strlen(a);
len = lena + strlen(b) + 1;
if (adata == 1)
{
if ((NULL != (c = (char *) REALLOC(a, len))))
{
strcpy (c + lena, b);
adata = 0;
}
else
{
SLang_Error = SL_MALLOC_ERROR;
return;
}
}
else if (NULL != (c = (char *) MALLOC(len)))
{
strcpy(c, a);
strcpy(c + lena, b);
}
else
{
SLang_Error = SL_MALLOC_ERROR; /* SLang_doerror("Lang Malloc error."); */
return;
}
/* instead of going throug push string, push it directly */
SLang_push_malloced_string(c);
if (adata == 1) FREE(a);
if (bdata == 1) FREE(b);
}
void SLdo_strtrim(void)
{
char *a, *beg, *end, *c, *neew, ch;
int len;
int adata;
if (SLang_pop_string(&a, &adata)) return;
len = strlen(a);
beg = a;
end = a + (len - 1);
while (ch = *beg, (ch == ' ') || (ch == '\t') || (ch == '\n')) beg++;
while (end >= beg)
{
ch = *end;
if ((ch == ' ') || (ch == '\t') || (ch == '\n')) end--;
else break;
}
end++;
len = (int) (end - beg);
/* instead of going throug push string, push it directly. This is because
* the string that is being copied might be a constant. */
if (NULL != (c = (char *) MALLOC(len + 1)))
{
neew = c;
while (beg < end) *c++ = *beg++;
*c = 0;
SLang_push_malloced_string(neew);
}
else SLang_Error = SL_MALLOC_ERROR; /* SLang_doerror("Lang Malloc error."); */
if (adata == 1) FREE(a);
}
static char Utility_Char_Table [256];
static void set_utility_char_table (char *pos)
{
register char *t = Utility_Char_Table, *tmax;
register unsigned char ch;
tmax = t + 256;
while (t < tmax) *t++ = 0;
t = Utility_Char_Table;
while ((ch = (unsigned char) *pos++) != 0) t[ch] = 1;
}
/* This routine returns the string with text removed between single character
comment delimeters from the set b and e. */
static void uncomment_string (char *str, char *b, char *e)
{
unsigned char chb, che;
unsigned char *s, *cbeg, *mark;
if (strlen(b) != strlen(e))
{
SLang_doerror ("Comment delimeter length mismatch.");
return;
}
set_utility_char_table (b);
if (NULL == (str = (char *) SLmake_string(str))) return;
s = (unsigned char *) str;
while ((chb = *s++) != 0)
{
if (Utility_Char_Table [chb] == 0) continue;
mark = s - 1;
cbeg = (unsigned char *) b;
while (*cbeg != chb) cbeg++;
che = (unsigned char) *(e + (int) (cbeg - (unsigned char *) b));
while (((chb = *s++) != 0) && (chb != che));
if (chb == 0)
{
/* end of string and end not found. Just truncate it a return; */
*mark = 0;
break;
}
strcpy ((char *) mark, (char *)s);
s = mark;
}
SLang_push_malloced_string (str);
}
void SLquote_string ()
{
char *str, *quotes, *q;
int sdata, qdata;
int slash, n;
register char *t, *s, *q1;
register unsigned char ch;
if ((SLang_pop_integer (&slash)) /* quote char */
|| (SLang_pop_string ("es, &qdata))) return; /* chars to quote */
if (SLang_pop_string (&str, &sdata)) /* string to quote */
{
if (qdata == 1) FREE (quotes);
return;
}
/* setup the utility table to have 1s at quote char postitions. */
set_utility_char_table (quotes);
t = Utility_Char_Table;
t[(unsigned int) slash] = 1;
/* calculate length */
s = str;
n = 0;
while ((ch = (unsigned char) *s++) != 0) if (t[ch]) n++;
n += (int) (s - str);
if (NULL != (q = (char *) MALLOC(n)))
{
s = str; q1 = q;
while ((ch = (unsigned char) *s++) != 0)
{
if (t[ch]) *q1++ = slash;
*q1++ = (char) ch;
}
*q1 = 0;
SLang_push_malloced_string(q);
}
else SLang_Error = SL_MALLOC_ERROR;
if (qdata == 1) FREE (quotes);
if (sdata == 1) FREE (str);
}
/* returns the position of substrin in a string or null */
void SLdo_issubstr(void)
{
char *a, *b, *c;
int adata, bdata, n;
if (SLang_pop_string(&b, &bdata) || SLang_pop_string(&a, &adata)) return;
if (NULL == (c = (char *) strstr(a, b))) n = 0; else n = 1 + (int) (c - a);
if (adata == 1) FREE(a);
if (bdata == 1) FREE(b);
SLang_push_integer (n);
}
/* returns to stack string at pos n to n + m of a */
void SLdo_substr(void)
{
char *a;
int adata, n, m;
char b[256];
if (SLang_pop_integer(&m) || SLang_pop_integer(&n) || (SLang_pop_string(&a, &adata))) return;
*b = 0;
if (m > 0)
{
strncpy(b, a + (n - 1), 254);
if (m > 255) m = 255;
b[m] = 0;
}
if (adata == 1) FREE(a);
SLang_push_string(b);
}
/* substitute char m at positin string n in string*/
void SLdo_strsub(void)
{
char *a;
int adata, n, m;
char b[256];
if (SLang_pop_integer(&m) || SLang_pop_integer(&n) || (SLang_pop_string(&a, &adata))) return;
strncpy(b, a, 254);
b[254] = 0;
if (adata == 1) FREE(a);
if ((n < 1) || (n > 254)) n = 254;
b[n-1] = (char) m;
SLang_push_string(b);
}
void SLdo_strup(void)
{
unsigned char c, *a;
int adata;
if (SLang_pop_string((char **) &a, &adata)) return;
SLang_push_string((char *) a);
if (adata == 1) FREE(a);
a = (unsigned char *) (SLStack_Pointer - 1)->v.s_val;
while ((c = *a) != 0)
{
/* if ((*a >= 'a') && (*a <= 'z')) *a -= 32; */
*a = UPPER_CASE(c);
a++;
}
}
void SLdo_strlow(void)
{
unsigned char c, *a;
int adata;
if (SLang_pop_string((char **) &a, &adata)) return;
SLang_push_string((char *) a);
if (adata == 1) FREE(a);
a = (unsigned char *) (SLStack_Pointer - 1)->v.s_val;
while ((c = *a) != 0)
{
/* if ((*a >= 'a') && (*a <= 'z')) *a -= 32; */
*a = LOWER_CASE(c);
a++;
}
}
void SLdo_strcmp(void)
{
char *a, *b;
int adata, bdata, i;
if (SLang_pop_string(&b, &bdata) || SLang_pop_string(&a, &adata)) return;
i = strcmp(a, b);
if (adata == 1) FREE(a);
if (bdata == 1) FREE(b);
SLang_push_integer (i);
}
void SLdo_strncmp(void)
{
char *a, *b;
int adata, bda